package com.douqu.game.core.config;

import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_TimeLimitActivityType;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author wangzhenfei
 *         2017-12-14 15:13
 */
public class TimeLimitedActivitiesConfig extends GameObject{
    /**类型**/
    public int type;

    /**最小等级**/
    public GoodsData[] openTime;

    /**奖励和条件**/
    public GoodsData[] reward;

    public int show;

    public int[] extraCondition;

    public List<Integer> serverIds;
    public List<Integer> platformIds;

    @Override
    public boolean check()
    {
        if(openTime == null)
        {
            LogUtils.error("TimeLimitedActivitiesConfig check openTime is null -> id:" + id + " name:" + name);
            return false;
        }
        return true;
    }


    @Override
    public boolean setVariable(String key, String value)
    {
        if("openTime".equals(key))
        {
            if(CoreUtils.isErrorValue(value)){
                openTime = new GoodsData[0];
            }
            openTime = LoadUtils.loadGoodDataToArray(key, value);
            return true;
        }else if("reward".equals(key)){
            reward = LoadUtils.loadGoodDataToArray(key, value);
            for(GoodsData data : reward){
                if(!data.checkGoods()){
                    LogUtils.error("TimeLimitedActivitiesConfig reward is error -> id:" + id + " name:" + name);
                    return false;
                }
            }
            if(type == E_TimeLimitActivityType.TOTAL_RECHARGE.getCode() && reward.length > 0){
                if(reward[0].extraValue == null || reward[0].extraValue.length == 0){
                    LogUtils.error("TimeLimitedActivitiesConfig reward.extraValue is error -> id:" + id + " name:" + name);
                    return false;
                }
            }
            return true;
        }else if("extraCondition".equals(key)){
            extraCondition = LoadUtils.loadIntArray(key, value);
            return true;
        }else if("serverIds".equals(key)){
            serverIds = new ArrayList<>();
            int[] result  = LoadUtils.loadIntArray(key, value);
            if(result != null){
                for(int id : result){
                    serverIds.add(id);
                }
            }
            return true;
        }else if("platformIds".equals(key)){
            platformIds = new ArrayList<>();
            int[] result = LoadUtils.loadIntArray(key, value);
            if(result != null){
                for(int id : result){
                    platformIds.add(id);
                }
            }
            return true;
        }
        else
            return super.setVariable(key, value);
    }


    public boolean isTimeIn(){
        if(openTime.length == 0){
            return false;
        }
        for(GoodsData data : openTime){
            long start = TimeUtils.getDateByString(data.type + "", TimeUtils.YYYYMMDD).getTime();
            long end = TimeUtils.getDateByString(data.id + "", TimeUtils.YYYYMMDD).getTime();
            if(DataFactory.currentTime > start && DataFactory.currentTime < end){
                return true;
            }
        }
        return false;
    }

    public boolean isTimeInForReceiver(){
        if(openTime.length == 0){
            return false;
        }
        for(GoodsData data : openTime){
            long start = TimeUtils.getDateByString(data.type + "", TimeUtils.YYYYMMDD).getTime();
            long end = TimeUtils.getDateByString(data.id + "", TimeUtils.YYYYMMDD).getTime() + CoreUtils.ONT_DAT;
            if(DataFactory.currentTime > start && DataFactory.currentTime < end){
                return true;
            }
        }
        return false;
    }

    public boolean isLastDay(Calendar calendar){
        if (openTime.length == 0)
            return false;
        long currentTime = calendar.getTime().getTime();
        for (GoodsData data : openTime){
            long end = TimeUtils.getDateByString(data.id + "", TimeUtils.YYYYMMDD).getTime() + CoreUtils.ONT_DAT;
            //如果当前时间和结束时间相差在24 小时以内则执行 因为 calendar增加过一分钟 所以一定会执行
            if (currentTime - end > 0 && currentTime - end < CoreUtils.ONT_DAT)
                return true;
         }
         return false;
    }

    public boolean isShow(int serverId){
        if(!isServerIn(serverId)){
            return false;
        }

        if(isTimeInForReceiver()){
            return true;
        }
        if(openTime.length == 0){
            return false;
        }

        if(show == 1){
//            if(isTimeInForReceiver()){
//                return true;
//            }
            for(GoodsData data : openTime){
                long start = TimeUtils.getDateByString(data.type + "", TimeUtils.YYYYMMDD).getTime();
                if(DataFactory.currentTime < start){
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isServerIn(int serverId){
        if(serverIds.contains(0)){
            return true;
        }
        return serverIds.contains(serverId);
    }

    public boolean isPlatformIn(int serverRuldId){
        if(platformIds.contains(0)){
            return true;
        }
        return platformIds.contains(serverRuldId);
    }

    public static boolean isShowIcon(int serverId, Player player){
        if(!CoreUtils.isFunctionUnLock(FunctionFactory.LIMIT_ACTIVITY, player))
            return false;

        List<TimeLimitedActivitiesConfig> configs = DataFactory.getInstance().getDataList(DataFactory.TIME_LIMIT_ACTIVITY_KEY);
        for(TimeLimitedActivitiesConfig config : configs){
            if(config.isShow(serverId)){
                return true;
            }
        }
        return false;
    }

    public int getMinDateInt(){
        return openTime[0].type;
    }

    public int getMaxDateInt(){
        return openTime[0].id;
    }

    /**
     * 每种类型的同时最多只允许展示一条
     * @param type
     * @return
     */
    public static TimeLimitedActivitiesConfig getCurrentConfigByType(E_TimeLimitActivityType type){
        List<TimeLimitedActivitiesConfig> timeLimitedActivitiesConfigs = DataFactory.getInstance().getDataList(DataFactory.TIME_LIMIT_ACTIVITY_KEY);
        //获取当前时间判断当前时间 在openTime的时间范围内
        for (TimeLimitedActivitiesConfig  timeLimitedActivitiesConfig:timeLimitedActivitiesConfigs){
            if (timeLimitedActivitiesConfig.type == type.getCode()
                    && timeLimitedActivitiesConfig.isTimeInForReceiver()){
                return timeLimitedActivitiesConfig;
            }
        }
        return null;
    }
}
